package leetcode_core.leetcode_1;

import java.util.Stack;

public class IsValid {
    public boolean isValid(String s){
        Stack<Character> stack = new Stack<>();
        for(int i = 0;i<s.length();i++){
            char c = s.charAt(i);
            if(c == '{' || c == '(' || c == '['){
                //左括号直接入栈
                stack.push(c);
            }else{
                if(!stack.isEmpty() && leftOf(c) == stack.peek()){
                    stack.pop();
                }else{
                    return false;
                }
            }

        }
        return stack.isEmpty();
    }

    private char leftOf(char c){
        if(c == '}'){
            return '{';
        }
        if(c == ']'){
            return '[';
        }
        return '(';
    }

}
